/**
 * 通话量页面数据获取
 * @author 王磊
 * @date 2019-1-12 14:25
 */
import request from '@/api/CallAmount/CallAmount'
import * as type from '@/store/Mutation/index'
import commonUtils from '@/assets/js/commonUtil.js'

const state = {
  header: [],
  body: {},
  callTotal: {},
  callTreeList: [],
  bluryList: [],
  classification: [],
  levelList: [],
  loading: false,
  totalSize: null,
  departmentName: '',
  downLoading: false,
  paginationLoading: false
}

const getters = {
  callTreeList: state => state.callTreeList,
  bluryList: state => state.bluryList,
  classification: state => state.classification
}

const mutations = {
  // 获取通话量头部
  [type.CALL_HEADER] (state, header) {
    state.header = header
  },
  // 获取通话量表格数据
  [type.CALL_BODY] (state, body) {
    state.body = body
  },
  // 获取通话量表格数据合计
  [type.CALL_TABLE_TOTAL](state, callTotal) {
    state.callTotal = callTotal
  },
  // 通话量列表树形结构数据
  [type.CALL_TREE_LIST] (state, tree) {
    state.callTreeList = tree
  },
  // 通话量列表树形结构数据
  [type.FUZZY_SEARCH] (state, tree) {
    state.callTreeList = tree
  },
  // 通话量列表树形结构表格搜索出的数据
  [type.FUZZY_CONTENT] (state, tree) {
    state.classification = tree
  },
  // 获取职级字典
  [type.LEVEL_LIST] (state, level) {
    state.levelList = level
  },
  // 表格加载状态
  [type.TABLE_LOADING_TYPE] (state, val) {
    state.loading = val
  },
  // 分页加载状态
  [type.PAGINATION_LOADING] (state, val) {
    state.paginationLoading = val
  },
  // 总条数
  [type.TOTAL_SIZE] (state, size) {
    state.totalSize = size
  },
  //下载等待状态
  [type.DOWN_LOADING] (state, val) {
    state.downLoading = val
  }
}

const actions = {
  /**
   * @author 王磊
   * @information 获取通话量头部数据
   * @date 2019-1-12
   * @param {*} commit 提交到mutation的数据
   */
  getCallAmountHeader ({ commit }) {
    request.getCallAmountHeader().then(res => {
      commit(type.CALL_HEADER, res.data)
    })
  },
  /**
    * @author 陈涛
    * @information 获取通话量人表格头部数据
    * @date 2019-1-16
    * @param {*} commit 提交到mutation的数据
    */
  getCallPeopleHeader ({ commit }) {
    request.getCallPeopleHeader().then(res => {
      commit(type.CALL_HEADER, res.data)
    })
  },
  /**
   * @author 王磊
   * @information 获取通话量区表格数据
   * @date 2019-1-12
   * @param {*} commit 提交到mutation的数据
   */
  getCallAmountBody ({ commit }, model) {
    // 分页加载状态
    commit(type.TABLE_LOADING_TYPE, true)
    request.getCallAmountBody(model).then(res => {
      commit(type.CALL_BODY, res.data)
      // 表格加载状态
      commit(type.TABLE_LOADING_TYPE, false)
      // 分页加载状态
      commit(type.PAGINATION_LOADING, false)
    }).catch((error) => {
      commonUtils.setMessage('error', error.message)
      // 表格加载状态
      commit(type.TABLE_LOADING_TYPE, false)
      // 分页加载状态
      commit(type.PAGINATION_LOADING, false)
    })
  },
  /**
   * @author 陈涛
   * @information 获取通话量店表格数据
   * @date 2019-1-14
   * @param {*} commit 提交到mutation的数据
   */
  getCallStoreBody ({ commit }, data) {
    request.getCallStoreBody(data).then(res => {
      commit(type.CALL_BODY, res.data)
    })
  },
  /**
   * @author 陈涛
   * @information 获取通话量组表格数据
   * @date 2019-1-15
   * @param {*} commit 提交到mutation的数据
   */
  getCallGroupBody ({ commit }, data) {
    request.getCallGroupBody(data).then(res => {
      commit(type.CALL_BODY, res.data)
    })
  },
  /**
   * @author 陈涛
   * @information 获取通话量人表格数据
   * @date 2019-1-16
   * @param {*} commit 提交到mutation的数据
   */
  getCallPeopleBody ({ commit }, data) {
    // 表格加载状态
    commit(type.TABLE_LOADING_TYPE, true)
    request.getCallPeopleBody(data).then(res => {
      // 表格加载状态
      commit(type.TABLE_LOADING_TYPE, false)
      // 分页加载状态
      commit(type.PAGINATION_LOADING, false)
      commit(type.CALL_BODY, res.data)
    }).catch((error) => {
      commonUtils.setMessage('error', error.message)
      // 表格加载状态
      commit(type.TABLE_LOADING_TYPE, false)
      // 分页加载状态
      commit(type.PAGINATION_LOADING, false)
    })
  },
  /**
   * @author 王磊
   * @information 区店组分页函数
   * @date 2019-1-14
   * @param {*} commit 提交到mutation的数据
   */
  callAmountPageModel ({ dispatch, commit }, model) {
     // 表格加载状态
    commit(type.PAGINATION_LOADING, true)
    dispatch('getCallAmountBody', model)
  },
  /**
   * @author 王磊
   * @information 人分页函数
   * @date 2019-1-14
   * @param {*} commit 提交到mutation的数据
   */
  callPeoplePageModel ({ dispatch, commit }, model) {
    // 表格加载状态
   commit(type.PAGINATION_LOADING, true)
   dispatch('getCallPeopleBody', model)
 },
  /**
   * @author 王磊
   * @information 获取通话量合计
   * @date 2019-1-14
   * @param {*} commit 提交到mutation的数据
   */
  tableTotal ({ commit }, model) {
    request.tableTotal(model).then(res => {
      commit(type.CALL_TABLE_TOTAL, res.data)
    })
  },
  /**
   * @author 王磊
   * @information 获取树形部门结构数据
   * @date 2019-1-19
   * @param {*} commit 提交到mutation的数据
   */
  getTreeList ({ commit }, model) {
    request.getTreeList(model).then(res => {
      commit(type.CALL_TREE_LIST, res.data.result)
    })
  },
  /**
   * @author 陈涛
   * @information 树形图搜索功能
   * @date 2019-1-20
   * @param {*} commit 提交到mutation的数据
   */
  getSearchTreeList ({ commit }, data) {
    request.getSearchTreeList(data).then(res => {
      commit(type.FUZZY_SEARCH, res.data.result)
    })
  },
  /**
   * @author 陈涛
   * @information 树形图搜索功能
   * @date 2019-1-22
   * @param {*} commit 提交到mutation的数据
   */
  getTreeInformation ({ commit }, data) {
    request.getTreeInformation(data).then(res => {
      let data = res.data.result
      data ? data.splice(data.length, 1, {name: '人', code: null}) : ''
      commit(type.FUZZY_CONTENT, data)
    })
  },
  /**
   * @author 陈涛
   * @information 获取职级字典
   * @date 2019-1-22
   * @param {*} commit 提交到mutation的数据
   */
  getLevelList ({ commit }) {
    request.getLevelList().then(res => {
      commit(type.LEVEL_LIST, res.data.result)
    })
  },
  /**
   * @author 王磊
   * @information 导出excel
   * @date 2019-2-14
   * @param {*} commit 提交到mutation的数据
   */
  exportExcel: function ({ commit }, model) {
    const excelData = model.excel
    //等待动画开启
    commit(type.DOWN_LOADING, true)
    request.exportExcel(model)
      .then(res => {
        let startTime = excelData.startTime.replace(' 00:00:00', '')
        let endTime = excelData.endTime.replace(' 23:59:00', '')
        // 这里res.data是返回的blob对象
        // application/vnd.openxmlformats-officedocument.spreadsheetml.sheet这里表示xlsx类型
        // Blob对象：表示一个不可变、原始数据的类文件对象
        var blob = new Blob([res.data], { type: 'application/vnd.ms-excel;charset=utf-8' })
        var downloadElement = document.createElement('a')
        document.body.appendChild(downloadElement)
        downloadElement.download = `${startTime}至${endTime}${state.departmentName}通话记录`
        // 创建下载的链接
        var href = window.URL.createObjectURL(blob)
        //下载完成关闭等待动画
        commit(type.DOWN_LOADING, false)
        commonUtils.setMessage('success', '导出成功！',true)
        downloadElement.href = href
        // 点击下载
        downloadElement.click()
        // 下载完成移除元素
        document.body.removeChild(downloadElement)
        // 释放掉blob对象
        window.URL.revokeObjectURL(href)
      })
      .catch(error => {
        //出错关闭等待动画
        commit(type.DOWN_LOADING,false)
        commonUtils.setMessage('error', '下载失败请重试',true)
      })
  },
  /**
   * @author 王磊
   * @information 总条数
   * @date 2019-3-14
   * @param {*} commit 提交到mutation的数据
   */
  totalItem ({ commit }, model) {
    request.totalItem(model).then((res) => {
      commit(type.TOTAL_SIZE, res ? res.data.result : null)
    })
  },
  /**
   * @author 王磊
   * @information 发送后端token
   * @date 2019-3-14
   * @param {*} commit 提交到mutation的数据
   */
  sendToken ({ dispatch }, id) {
    request.sendToken().then((res) => {
      dispatch('getTreeList', id)
    })
  }
}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
